<template>
  <div>
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户：" prop="user">
        <el-input type="input" v-model="ruleForm.user" 
         autocomplete="on" 
         style="width:200px" 
         :disabled="readonly"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="password" >
        <!--el中按键绑定 使用 .native 替换其内部事件 -->
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
          style="width:200px"
          @keyup.enter.native="submitForm('ruleForm')" 
          :disabled="readonly"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" :disabled="readonly">
          {{ruleForm.context}}
        </el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-tag type="info" v-if="show">您尚未登陆</el-tag>
        <el-tag type="success" v-else>{{ruleForm.welcome}}</el-tag>
        <el-button type="danger" plain style="margin-left:20px;" v-show="!show" @click="quit">
          退出登录
          <i class="el-icon-circle-close"></i>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        user: "",
        password: "",
        context: "提交",
        welcome: ""
      },
      show: true,
      flag:true,
      readonly:false,
      userMessage: [
        { id: 1, name: "张三", pw: "123456" },
        { id: 2, name: "李四", pw: "123456" },
        { id: 3, name: "admin", pw: "123456" }
      ],
      loginUser:''  , //存储登录的用户
    };
  },
  created() {
    this.getUserState();
    this.stateInit();
  },
   watch:{
    // 直接监听路由，通过router-link换url，可以将组建对应跳转
    $route(to,from){},
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.userMessage.forEach(item => {
            if (item.name == this.ruleForm.user && item.pw == this.ruleForm.password) {
              this.show = false;
              return false;
            }
          });

          if (!this.show && this.flag) {
            this.ruleForm.welcome = "您好，" + this.ruleForm.user;
            this.loginUser =this.ruleForm.user;
            this.setUserState();
            alert("登录成功，" + this.ruleForm.welcome);
            this.resetForm(formName);
            this.flag =false
            this.readonly =true
            this.$store.commit('increment',this.loginUser)
          } else {
            alert("用户名或者密码错误！");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    quit() {
      this.show = true
      this.loginUser =''
      this.setUserState();
      this.flag =true;
      this.readonly =false;
      this.$store.commit('increment','')
    },

    getUserState() {
      if (localStorage.getItem("loginUser")) {
        try {
          this.loginUser = JSON.parse(localStorage.getItem("loginUser"));
          this.$store.commit('increment',this.loginUser)
        } catch (error) {
          console.log(error);
          localStorage.setItem(
            "loginUser",
            JSON.stringify(this.loginUser)
          );
        }
      } else {
        localStorage.setItem(
          "loginUser",
          JSON.stringify('')
        );
      }
    },

    setUserState(){
      localStorage.setItem(
          "loginUser",
          JSON.stringify(this.loginUser)
      );
    },

    stateInit(){
      if(this.loginUser!=''){
        this.show = false
        this.ruleForm.welcome = "您好，" + this.loginUser
        this.flag =false;
        this.readonly =true;
      }
    }
  }
};
</script>

<style scoped>
.demo-ruleForm {
  margin-top: 30px;
}
</style>